const express = require('express');
const robot = require('./service/robot');
const app = express();
const globalConfig = require('./config/global');
const robotConfig = require('./config/baidu');
const wechatConfig = require('./config/weixin');
const wechat = require('./service/wechat')(wechatConfig.token);
let sessonIdMap = { //存放sessId用的

}

/**
 * 机器人接口
 */
app.get('/web/chat', (req, res) => {
    const {query} = req;
    const {message, userId} = query;
    robot.chatToRobot({
        message ,  
        serviceId : robotConfig.SERVICE_ID,
        skillIds : robotConfig.BOT_IDS,
        userId
    }).then(json => {
        const {result, data} = json;
        let msg = '没有命中';
        if(result == 0 && data['error_code'] == 0) {
            const {'result' : robotData} = data; 
            const {'response_list' : responseList} = robotData;
            const {'action_list' : actionList} = responseList && responseList[0] || {};
            
            if(actionList && actionList.length) {
                const aciton = actionList[Math.floor(Math.random() * actionList.length)];
                msg = aciton && aciton.say;
            }
            //return res.end(msg);
        }
        res.end(JSON.stringify(data));
    }).catch(e => {
        res.end(e);
    });
});

/**
 * 提供给微信调用
 */
app.use('/weixin/chat', require('./router/weixin').route);


app.listen(globalConfig.port);


